/**
 * 
 */
package com.yinhai.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.lz.util.Utils;
import com.yinhai.adapter.CommonADContentListAdapter;
import com.yinhai.cfg.LoginConfig;
import com.yinhai.cfg.LoginConfigUtil;
import com.yinhai.constant.UrlConstant;
import com.yinhai.model.CommonAD;
import com.yinhai.service.NoticeNewsServiceImpl;

/**
 * @author lz
 * 
 * 
 */
public class CommonADContentActivity extends Activity {
	public String phone;
	private ImageView news_btn;
	private AQuery aq;
	LinearLayout layoutll = null;
	NoticeNewsServiceImpl noticedao = null;
	CommonADContentActivity thisRef = null;

	CommonADContentListAdapter listadpter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_news);
		thisRef = this;
		aq = new AQuery(thisRef);
		aq.id(R.id.title_txt).text("企业公众信息");
		aq.id(R.id.ll_news).backgroundColor(Color.parseColor("#ebebeb"));
		LoginConfig log = LoginConfigUtil.getConfig(thisRef);
		phone = log.getUsername();

		listadpter = new CommonADContentListAdapter(thisRef);
		aq.id(R.id.message_list).adapter(listadpter);

		findNotices();

		news_btn = (ImageView) findViewById(R.id.news_btn);
		news_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

	}

	private void findNotices() {
		String url = UrlConstant.LOCAL_URI + "findMessageNotices";
		Map<String, String> params = new HashMap<String, String>();
		params.put("phone", phone);
		aq.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {
			@Override
			public void callback(String url, JSONObject json, AjaxStatus status) {
				try {
					JSONArray js;
					js = json.getJSONArray("data");
					if (js.length() > 0) {
						for (int i = 0; i < js.length(); i++) {
							JSONObject object = js.getJSONObject(i);
							if (i == 0) {
								Utils.SaveADVersionNUM(thisRef, Integer
										.parseInt(object.getString("version")));
							}
							CommonAD u = new CommonAD();
							u.setId(object.getString("id"));
							u.setTitle(object.getString("title"));
							u.setImg(object.getString("img"));
							u.setSubTime(object.getString("subTime"));
							u.setUrl(object.getString("url"));

							listadpter.addItem(u);
						}
					}

					listadpter.notifyDataSetChanged();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

}
